// pages/index/index.js
Page({
  data: {
    attractions: [
      {
        id: 1,
        name: '龙象塔',
        description: '青秀山标志性建筑，登塔可俯瞰南宁全景',
        image: 'https://tse1-mm.cn.bing.net/th/id/OIP-C.5_kPdcgOETPuNDoYfOF67AHaEK?w=295&h=180&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3',
        tags: ['标志建筑', '观景台']
      },
      {
        id: 2,
        name: '天池',
        description: '碧波荡漾的人工湖，可乘船游览',
        image: 'https://tse1-mm.cn.bing.net/th/id/OIP-C.3BsX_ffNMdAwDOCj24lR7AHaEd?w=314&h=187&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3',
        tags: ['湖泊', '游船']
      },
      {
        id: 3,
        name: '苏铁园',
        description: '国内最大的苏铁专类园',
        image: 'https://tse1-mm.cn.bing.net/th/id/OIP-C.rQn-TLTTwGSmieJeegBudAHaEw?w=233&h=180&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3',
        tags: ['植物园', '科普']
      },
      {
        id: 4,
        name: '观音禅寺',
        description: '历史悠久的佛教寺庙',
        image: 'https://tse3-mm.cn.bing.net/th/id/OIP-C.Y07-ZTnGpd17ACC_SP1hJQHaFJ?w=220&h=180&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3',
        tags: ['寺庙', '文化']
      },
      {
        id: 5,
        name: '友谊长廊',
        description: '连接各景点的美丽长廊',
        image: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.mrWw44J5-Z0Ztx1x2MK9KQHaE8?w=257&h=180&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3',
        tags: ['长廊', '休闲']
      },
      {
        id: 6,
        name: '棕榈园',
        description: '热带风情植物园区',
        image: 'https://tse1-mm.cn.bing.net/th/id/OIP-C.RT3K81ZtraSdAg6K387PNwHaEo?w=311&h=194&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3',
        tags: ['植物', '热带']
      },
      {
        id: 7,
        name: '荷花池',
        description: '夏季赏荷的绝佳地点',
        image: 'https://tse1-mm.cn.bing.net/th/id/OIP-C.yvixusiNaXBiCrLbnmjS2AHaEq?w=293&h=185&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3',
        tags: ['花卉', '摄影']
      },
      {
        id: 8,
        name: '儿童乐园',
        description: '亲子游玩的好去处',
        image: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.twuKL_TlWdsGYnf_sg5O-wHaEn?w=249&h=180&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3',
        tags: ['亲子', '娱乐']
      },
      {
        id: 9,
        name: '桃花岛',
        description: '春季赏桃花的绝佳地点',
        image: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.-S1irxnSl3t9jhkdap1WFQHaFZ?w=244&h=180&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3',
        tags: ['花卉', '春季'],
        type: '景点'
      },
      {
        id: 10,
        name: '凤凰岭',
        description: '登山观景的好去处',
        image: 'https://tse3-mm.cn.bing.net/th/id/OIP-C.DguxQ5MH5M2l4fj8IOWEJgHaE8?w=240&h=180&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3',
        tags: ['登山', '观景'],
        type: '景点'
      }
    ],
    activities: [
      {
        id: 101,
        name: '春季花展',
        description: '每年春季举办的盛大花卉展览',
        image: 'https://tse3-mm.cn.bing.net/th/id/OIP-C.UU0j4E5YPYgTuIuyKl_V-AHaE_?w=239&h=180&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3',
        tags: ['花展', '春季'],
        type: '活动',
        time: '3月-5月'
      },
      {
        id: 102,
        name: '中秋灯会',
        description: '传统中秋佳节灯会活动',
        image: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.Bw0JMbyi1aCwqH3XLwqwzgHaEW?w=311&h=183&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3',
        tags: ['灯会', '中秋'],
        type: '活动',
        time: '9月-10月'
      },
      {
        id: 103,
        name: '摄影大赛',
        description: '青秀山风光摄影比赛',
        image: 'https://tse1-mm.cn.bing.net.th/id/OIP-C.ghi789cameraHaE8?w=257&h=180&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3',
        tags: ['摄影', '比赛'],
        type: '活动',
        time: '全年'
      },
      {
        id: 104,
        name: '老友粉',
        description: '南宁特色美食',
        image: 'https://tse1-mm.cn.bing.net.th/id/OIP-C.ghi789cameraHaE8?w=257&h=180&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3',
        tags: ['美食'],
        type: '活动',
        time: '全年'
      },
    ],
    foods: [
      {
        id: 201,
        name: '青秀山绿茶',
        description: '本地特色绿茶，清香甘醇',
        image: 'https://tse3-mm.cn.bing.net.th/id/OIP-C.jkl012teaHaE8?w=257&h=180&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3',
        tags: ['饮品', '特产'],
        type: '美食',
        price: '￥38'
      }
    ],
    searchValue: '',
    searchResults: [],
    isSearching: false,
    showSearchResults: false,
    routes: [],
    allRoutes: [
      {
        id: 1,
        index: '1',
        name: '经典半日游',
        duration: '3-4小时',
        distance: '3公里',
        content: '北门 → 龙象塔 → 天池 → 苏铁园 → 观音禅寺 → 友谊长廊',
        tips: '适合时间紧张的游客'
      },
      {
        id: 2,
        index: '2',
        name: '休闲一日游',
        duration: '6-7小时',
        distance: '5公里',
        content: '西门 → 友谊长廊 → 观音禅寺 → 天池（午餐）→ 苏铁园 → 龙象塔',
        tips: '推荐在天池周边用餐'
      },
      {
        id: 3,
        index: '3',
        name: '亲子游路线',
        duration: '4-5小时',
        distance: '2.5公里',
        content: '北门 → 儿童乐园 → 友谊长廊 → 天池（游船）→ 苏铁园',
        tips: '包含儿童娱乐项目'
      },
      {
        id: 4,
        index: '4',
        name: '深度文化游',
        duration: '5-6小时',
        distance: '4公里',
        content: '西门 → 观音禅寺 → 龙象塔 → 天池 → 友谊长廊 → 苏铁园',
        tips: '深度体验青秀山文化'
      },
      {
        id: 5,
        index: '5',
        name: '自然风光游',
        duration: '4-5小时',
        distance: '3.5公里',
        content: '北门 → 棕榈园 → 荷花池 → 天池 → 龙象塔',
        tips: '适合摄影爱好者'
      },
      {
        id: 6,
        index: '6',
        name: '健身徒步游',
        duration: '3-4小时',
        distance: '4.5公里',
        content: '西门 → 龙象塔 → 天池 → 苏铁园 → 棕榈园 → 北门',
        tips: '适合健身爱好者'
      }
    ],
    showAllAttractions: false,
    initialAttractionsCount: 6,
    showAllRoutes: false,
    displayedRoutesCount: 3
  },

  onLoad: function (options) {
    console.log('页面加载完成');
    this.setData({
      routes: this.data.allRoutes.slice(0, this.data.displayedRoutesCount)
    });
  },

  // 景点详情点击事件 - 确保这个方法在最外层
  viewAttractionDetail: function(e) {
    console.log('=== 点击景点事件开始 ===');
    console.log('事件对象:', e);
    
    const id = e.currentTarget.dataset.id;
    console.log('获取到的ID:', id, '类型:', typeof id);
    
    // 查找景点
    const attraction = this.data.attractions.find(item => {
      console.log('比较:', item.id, typeof item.id, '==', id, typeof id);
      return item.id == id; // 使用 == 允许类型转换
    });
    
    console.log('找到的景点:', attraction);
    
    if (attraction) {
      console.log('显示景点详情');
      wx.showModal({
        title: '🎯 ' + attraction.name,
        content: attraction.description + '\n\n标签：' + attraction.tags.join('、'),
        showCancel: false,
        confirmText: '知道了',
        confirmColor: '#4caf50'
      });
    } else {
      console.error('未找到景点');
      wx.showToast({
        title: '景点信息加载失败',
        icon: 'none'
      });
    }
  },

  // 其他方法保持不变
  toggleAttractions: function() {
    this.setData({
      showAllAttractions: !this.data.showAllAttractions
    });
    
    wx.showToast({
      title: this.data.showAllAttractions ? '已展开所有景点' : '已收起部分景点',
      icon: 'none',
      duration: 1000
    });
  },

  onSearchInput: function(e) {
    const value = e.detail.value;
    this.setData({
      searchValue: value
    });
    
    if (!value.trim()) {
      this.setData({
        showSearchResults: false,
        isSearching: false
      });
      return;
    }
    
    this.setData({
      isSearching: true
    });
    
    this.performSearch(value.trim());
  },

  performSearch: function(keyword) {
    const allItems = [
      ...this.data.attractions,
      ...this.data.activities,
      ...this.data.foods,
      ...this.data.allRoutes
    ];
    
    const results = allItems.filter(item => {
      if (!item) return false;
      const searchFields = [
        item.name || '',
        item.description || '',
        item.tags ? item.tags.join(' ') : '',
        item.type || '',
        item.content || ''
      ];
      const searchText = searchFields.join(' ').toLowerCase();
      const searchKeyword = keyword.toLowerCase();
      return searchText.includes(searchKeyword);
    });
    
    this.setData({
      searchResults: results,
      showSearchResults: true
    });
  },

  clearSearch: function() {
    this.setData({
      searchValue: '',
      searchResults: [],
      showSearchResults: false,
      isSearching: false
    });
  },

  onSearchFocus: function() {
    if (this.data.searchValue) {
      this.setData({
        showSearchResults: true
      });
    }
  },

  onSearchBlur: function() {
    setTimeout(() => {
      if (!this.data.keepResultsOpen) {
        this.setData({
          showSearchResults: false
        });
      }
    }, 300);
  },

  onSearchResultTap: function(e) {
    this.setData({
      keepResultsOpen: true
    });
    
    const item = e.currentTarget.dataset.item;
    let content = '';
    if (item.type === '路线') {
      content = `时长：${item.duration}\n距离：${item.distance}\n路线：${item.content}\n提示：${item.tips}`;
    } else {
      content = `${item.description}\n类型：${item.type}`;
      if (item.time) content += `\n时间：${item.time}`;
      if (item.price) content += `\n价格：${item.price}`;
    }
    
    wx.showModal({
      title: item.name,
      content: content,
      showCancel: false,
      confirmText: '知道了',
      complete: () => {
        this.setData({
          keepResultsOpen: false,
          showSearchResults: false
        });
      }
    });
  },

  onSearchConfirm: function(e) {
    const value = e.detail.value;
    this.setData({
      searchValue: value
    });
    
    if (!value.trim()) {
      this.setData({
        showSearchResults: false,
        isSearching: false
      });
      return;
    }
    
    this.setData({
      isSearching: true
    });
    
    this.performSearch(value.trim());
  },

  viewAllRoutes: function() {
    if (this.data.showAllRoutes) {
      this.setData({
        routes: this.data.allRoutes.slice(0, this.data.displayedRoutesCount),
        showAllRoutes: false
      });
      wx.showToast({
        title: '已收起路线',
        icon: 'success'
      });
    } else {
      this.setData({
        routes: this.data.allRoutes,
        showAllRoutes: true
      });
      wx.showToast({
        title: '显示所有路线',
        icon: 'success'
      });
    }
  },

  viewRouteDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    const route = this.data.allRoutes.find(item => item.id === id);
    wx.showModal({
      title: route.name,
      content: `时长：${route.duration}\n距离：${route.distance}\n路线：${route.content}\n提示：${route.tips}`,
      showCancel: false,
      confirmText: '知道了'
    });
  },

  navigateToMap: function() {
    wx.showToast({
      title: '跳转景区地图',
      icon: 'none'
    });
  },

  navigateToTransport: function() {
    wx.showToast({
      title: '跳转交通指南',
      icon: 'none'
    });
  },

  navigateToLostFound: function() {
    wx.showToast({
      title: '跳转失物招领',
      icon: 'none'
    });
    wx.navigateTo({
      url: '/pages/LostFound/LostFound'
    });
  },

  navigateToTickets: function() {
    wx.showToast({
      title: '跳转门票预订',
      icon: 'none'
    });
  },

  navigateToFood: function() {
    wx.showToast({
      title: '跳转特色美食',
      icon: 'none'
    });
    wx.navigateTo({
      url: '/pages/Food/Food'
    });
  },

  navigateToTreasure: function() {
    wx.showToast({
      title: '跳转寻宝',
      icon: 'none'
    });
    wx.navigateTo({
      url: '/pages/treasure/treasure'
    });
  }
})